// pages/me/myWallet/myWallet.js
import { LuckyBeansHttp } from '../../../api/luckyBeans'
import Utils from '../../../utils/utils'

const luckyBeansModel = new LuckyBeansHttp();

Page({
  initFlag:false,
  /**
   * 页面的初始数据
   */
  autoAuth:true,
  data: {
    luckyBeans: 0,
    billList: [], // 账单列表
    rate: 1,
    pea_estimate: 1,
    page: 1,
    pageSize: 10,
    totalNum: 0,
    ServiceText:false
  },

  initPage () {
    this.httpGetLuckyBeansBill()
  },
  onHide(){
    this.initFlag = true;
  },
  onShow(){
    if(this.initFlag === true){
      this.initFlag = false;
      this.httpGetLuckyBeansBill(true);
    }
  },
  // 提现
  onWithdraw () {
    Utils.toPage('/pages/me/withdrawal/index');
    // Utils.msg('请下载巷友APP，进行提现！')
  },

  //页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    });
    this.httpGetLuckyBeansBill();
  },

  // 页面上拉触底事件的处理函数
  // onReachBottom: function () {
  //   this.setData({
  //     page: this.data.page + 1
  //   })
  //   this.httpGetLuckyBeansBill()
  // },

  httpGetLuckyBeansBill () {
    let self = this;
    const { page, pageSize, billList } = this.data;
    if(page === 1){
      Utils.request({
        url:'/s/userinfo/PeaGoodsList',
        loading:2,
        callback:function (result) {
          self.setData({
            luckyBeans:result.res.pea_blance,
            rate:result.res.rate,
            pea_estimate:result.res.pea_estimate,
          });
        }
      });
    }
    luckyBeansModel.getLuckyBeansBill({
      page: page,
      limit: pageSize
    }).then((res) => {
      wx.stopPullDownRefresh();
      this.setData({
        luckyBeans: res.pea_blance,
        billList: page === 1 ? res.list : billList.concat(res.list),
        totalNum: res.totalcount
      })
    })
  },
});
